//
//  HttpManager.m
//  One
//
//  Created by mac on 16-5-20.
//  Copyright (c) 2016年 mac. All rights reserved.
//

#import "HttpManager.h"

@implementation HttpManager

+ (void)requestDataWithParameters:(NSDictionary *)parameterDictionary requestMethod:(NSString *)httpMethod completionBlock:(CompletionBlock)block
{
    //1.reachWeb
    [self reachWebStatus];
    
    NSString *keyStr = [NSString stringWithFormat:@"%@",parameterDictionary[URLKEY]];
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",APISERVICE_URL,keyStr];
    NSLog(@"%@",urlStr);
    UIWindow *window = [[[UIApplication sharedApplication]delegate]window];
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:window animated:YES];

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [hud hide:YES];
        NSDictionary *responseDic = (NSDictionary *)responseObject;
        block(responseDic);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"请求出错:%@",error);
        hud.labelText = @"出错啦";
        [hud hide:YES afterDelay:3];

    }];  
}
    
+ (void)reachWebStatus
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusUnknown || status == AFNetworkReachabilityStatusNotReachable) {
            [UIAlertController alertWithMessage:@"无网络" withActionBlock:nil];
        }
    }];
}


@end
